#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
int dp[N];
int v[N], w[N];
int t, m;
int main()
{
	cin >> t >> m;
	for (int i = 1; i <= m; i++) {
		cin >> v[i] >> w[i];
	}
	for (int i = 1; i <= m; i++) {
		for (int j = t; j >= v[i]; j--) {
			dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
		}
	}
	cout << dp[t] << endl;
	return 0;
}